package com.dc2013.adapter;


import java.util.ArrayList;
import java.util.List;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.dc2013.followme.ImageItem;
import com.dc2013.followme.R;
import com.dc2013.followme.R.id;
import com.dc2013.network.BitmapLruCache;
import com.dc2013.util.URLString;

import android.app.LauncherActivity.ListItem;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class LikeAdapter extends BaseAdapter
{
        Context context;
        ArrayList<ImageItem> lists;
        int layout;
        private LayoutInflater inflater;
        ViewHolder holder;
        public LikeAdapter(Context context,int layout,ArrayList<ImageItem> lists) 
        {
                this.context = context;
                this.layout = layout;
                this.lists = lists;
                inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() 
        {
                return lists.size();
        }
        @Override
        public Object getItem(int position) 
        {
                return lists.get(position);
        }
        
        @Override
        public long getItemId(int position) 
        {
                return position;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
                ImageItem item = lists.get(position);
                if(convertView == null)
                {
                        convertView = inflater.inflate(layout, parent,false);
                        holder = new ViewHolder();
                        holder.image = (ImageView)convertView.findViewById(R.id.list_img);
                        holder.subject_tv = (TextView)convertView.findViewById(R.id.subject_tv);
                        holder.comment_tv = (TextView)convertView.findViewById(R.id.comment_tv);
                        holder.like_btn = (ImageButton)convertView.findViewById(R.id.like_btn);
                        holder.share_btn = (ImageButton)convertView.findViewById(R.id.share_btn);
                        holder.queue = Volley.newRequestQueue(context);
            Response.Listener<Bitmap> listener = new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap bitmap) {
                    holder.image.setImageBitmap(bitmap);
                }
            };
            Response.ErrorListener errorListener = new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            };
                        holder.image_request = new  ImageRequest(item.getImageUrl(), listener, 1920, 1080, Bitmap.Config.ARGB_8888, errorListener);
                        holder.share_btn.setOnClickListener(new OnClickListener()
                        {
                                
                                @Override
                                public void onClick(View v)
                                {
                                        
                                        
                                }               //sharebutton 클릭
                        });
                        holder.like_btn.setOnClickListener(new OnClickListener() 
                        {
							
							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								
							}
						});

                        convertView.setTag(holder);
                }
                else
                        holder =(ViewHolder)convertView.getTag();
                if(item!=null){
                        
                        holder.subject_tv.setText(lists.get(position).getSubjectName().toString());
                        //holder.comment_tv.setText(Integer.toString(lists.get(position).getNumberOfComment()));
                       
                        
                }
                
                

                return convertView;
                
        }

        private static class ViewHolder
        {
                ImageView image;
                TextView subject_tv,comment_tv;
                ImageButton share_btn,like_btn;
                
                ImageLoader image_loader;
                RequestQueue queue;
                ImageRequest image_request;
                
        }
        void free(){
                holder.comment_tv=null;
                holder.image=null;
                holder.like_btn=null;
                holder.subject_tv=null;
                holder.share_btn=null;
                holder.like_btn = null;
                
        }
        @Override
        protected void finalize() throws Throwable 
        {
                // TODO 자동 생성된 메소드 스텁
                free();
                super.finalize();
        }
}